CREATE TABLE [dbo].[tb_AsignaturasGenericas]
(
[idAsignaturaGenerica] [int] NOT NULL IDENTITY(1, 1),
[Descripcion] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Tipo] [int] NULL,
[Mnemo] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tb_AsignaturasGenericas_Mnemo] DEFAULT ('N'),
[Baja] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tb_AsignaturasGenericas_Baja] DEFAULT ('N')
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[AddNumber3] ON [dbo].[tb_AsignaturasGenericas] 
FOR INSERT
AS
SET NOCOUNT ON
DECLARE @ID INT
DECLARE @IDV VARCHAR(5)

SELECT @ID = (SELECT IdAsignaturagenerica FROM INSERTED)
SELECT @IDV = RTRIM(Cast(@ID AS VARCHAR(5)))

SELECT @IDV = CASE LEN(@IDV)
WHEN 4 THEN '0' + @IDV
ELSE @IDV
END

/*UPDATE tb_AsignaturasGenericas SET Descripcion = @IDV +' - ' + Descripcion  
WHERE Idasignaturagenerica = @ID*/
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[deletetb_AsignaturasGenericas] ON [dbo].[tb_AsignaturasGenericas]
FOR DELETE AS 
		 
--DELETE tb_Asignaturas FROM tb_Asignaturas
--WHERE AsignaturaGenerica = (SELECT idAsignaturaGenerica FROM deleted)
SET NOCOUNT ON
if @@ERROR <> 0 
	ROLLBACK TRANSACTION
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_AsignaturasGenericas] ON [dbo].[tb_AsignaturasGenericas]
FOR UPDATE AS
SET NOCOUNT ON 
IF (update(idAsignaturaGenerica)) begin 
              SELECT * FROM inserted 
--	 IF @@ROWCOUNT = 1 begin 
--		 UPDATE tb_Asignaturas
--		 SET AsignaturaGenerica = (SELECT idAsignaturaGenerica FROM inserted)
--		 WHERE tb_Asignaturas.AsignaturaGenerica = (SELECT idAsignaturaGenerica FROM deleted)
--	end  ELSE begin 
--		 ROLLBACK TRANSACTION 
--end 
end
GO
ALTER TABLE [dbo].[tb_AsignaturasGenericas] ADD CONSTRAINT [PK_tb_AsignaturasGenericas] PRIMARY KEY CLUSTERED  ([idAsignaturaGenerica]) WITH (FILLFACTOR=100) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_AsignaturasGenericas] WITH NOCHECK ADD CONSTRAINT [FK_tb_asignaturasgenericas_tb_TiposEspaciosCurriculares] FOREIGN KEY ([Tipo]) REFERENCES [dbo].[tb_TiposEspaciosCurriculares] ([idTipoEspacioCurricular])
GO
